import api from '../../api';
import { message } from 'antd';
export default {
  namespace: 'couponPoolApprovalM',

  state: {
    poolNameData: [],
    selectData: {},
    currentUser: {},
    applyData: {},
    showData: { columns: [], dataSource: [], pageInfo: { currentPage: 0, numInPage: 20, total: 1 } },
    detailData: {},
    tagData: {},
    issuer: [],
    bringTagData: {},
    symbolData1: [
      {
        code: 'equal',
        id: 'flow_filter_operation_symbol_6',
        value: '等于',
      },
      {
        code: 'not_equal',
        id: 'flow_filter_operation_symbol_7',
        value: '不等于',
      },
    ],
    symbolData2: [
      {
        code: 'equal',
        id: 'flow_filter_operation_symbol_6',
        value: '等于',
      },
    ],
  },

  effects: {
    // 获取券池名称
    *searchContent({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.searchContent, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            issuer: res.data.dataSource,
          },
        });
      } else {
        message.error('获取内容失败');
      }
    },
    // 获取新增自动审批时维度为申万一级内容
    *searchContentOne({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.searchContentOne, payload);
      if (res && res.code == 0) {
        callback(res.data.dataSource);
      } else {
        callback();
        message.error('获取内容失败');
      }
    },
    // 获取新增自动审批时维度为申万二级内容
    *searchContentTwo({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.searchContentTwo, payload);
      if (res && res.code == 0) {
        callback(res.data.dataSource);
      } else {
        callback();
        message.error('获取内容失败');
      }
    },
    // 获取券池名称
    *getPoolName({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.getPoolName, payload);
      if (res && res.data) {
        yield put({
          type: 'save',
          payload: {
            poolNameData: res.data.dataSource,
          },
        });
      } else {
        message.error('获取券池名称失败');
      }
    },
    //查询行业和研究员
    *getIndustryAndRole({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.getIndustryAndRole, payload);
      if (res) {
        callback(res);
      } else {
        message.error('获取行业和研究员失败');
      }
    },
    //查询文件下载地址
    *queryDownLoadUrl({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.queryDownLoadUrl, payload);
      if (res) {
        callback(res);
      } else {
        callback();
      }
    },

    // 获取债券信息
    *getSecuritiesDetail({ payload, callback }, { call, put }) {
      const query = {
        tradingCode: payload.tradingCode,
        exchangeCode: payload.exchangeCode,
      };
      const res = yield call(api.getSecuritiesDetail[payload.type], query);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            threeDatas: res.data.dataSource,
          },
        });
        callback(res.data.dataSource);
      } else {
        message.error('获取债券信息失败');
      }
    },

    // 手动新增and修改
    *getAddAndEdit({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.getAdd, payload);
      if (res && res.code == 0) {
        callback(res);
        message.success('保存成功');
      } else {
        message.error(res.msg);
      }
    },
    // 自动新增and修改
    *autoAdd({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.autoAdd, payload);
      if (res && res.code == 0) {
        callback(res);
        message.success('保存成功');
      } else {
        message.error(res.msg);
      }
    },

    // 发起流程时校验
    *checkAdd({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.checkAdd, payload);
      if (res && res.code == 0) {
        callback(res);
      } else {
        message.error(res.msg);
      }
    },
    // 出池带出标签值
    *bringTag({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.bringTag, payload);
      if (res && res.code == 0) {
        callback(res.data.dataSource);
      } else {
        message.error(res.msg);
      }
    },

    //获取流程申请人
    *getApplyPeople({ payload }, { call, put }) {
      const res = yield call(api.poolApproval.getApplyPeople, payload);
      if (res && res.success == true) {
        yield put({
          type: 'save',
          payload: {
            applyData: res.data,
          },
        });
      } else {
        message.error('获取流程申请人失败');
      }
    },
    // 查询详细信息
    *getDetails({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.getDetails, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            detailData: res.data.dataSource,
          },
        });
        callback(res.data);
      } else {
        message.error('查询列表失败');
      }
    },
    //查询所有
    *findAll({ payload }, { call, put }) {
      const res = yield call(api.poolApproval.findAll, payload);
      if (res && res.code == 0) {
        yield put({
          type: 'save',
          payload: {
            showData: res.data,
          },
        });
      } else {
        message.error('查询列表失败');
      }
    },

    // 获取标签
    *findTag({ payload }, { call, put }) {
      const res = yield call(api.poolApproval.findTag, payload);
      if (res) {
        yield put({
          type: 'save',
          payload: {
            tagData: res,
          },
        });
      }
    },

    //查询内部评级
    *getInternalRating({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.getInternalRating, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        message.error('获取内部评级失败');
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
